package com.dodoke.service.impl;


import java.util.ArrayList;
import java.util.List;

import com.dodoke.common.core.domain.model.LoginUser;
import com.dodoke.common.utils.ServletUtils;
import com.dodoke.common.utils.bean.BeanUtils;
import com.dodoke.common.utils.spring.SpringUtils;
import com.dodoke.domain.WxGoods;
import com.dodoke.domain.WxGoodsAttr;
import com.dodoke.domain.WxImage;
import com.dodoke.domain.vo.WxGoodsAttrVo;
import com.dodoke.domain.vo.WxGoodsVo;
import com.dodoke.domain.vo.WxImageVo;
import com.dodoke.framework.web.service.TokenService;
import com.dodoke.mapper.WxGoodsAttrMapper;
import com.dodoke.mapper.WxGoodsMapper;
import com.dodoke.mapper.WxImageMapper;
import com.dodoke.service.IWxGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * 【商品】Service业务层处理
 *
 * @author dodoke
 * @date 2020-08-25
 */
@Service
public class WxGoodsServiceImpl implements IWxGoodsService {
    @Autowired
    private WxGoodsMapper wxGoodsMapper;



    @Override
    public WxGoods selectWxGoodsByName(String name) {
        return  wxGoodsMapper.selectWxGoodsByName(name);
    }

    /**
     * 通过商品id查询【商品】
     *
     * @param id 【商品】ID
     * @return 【商品信息】
     */
    @Override
    public WxGoods selectWxGoodsById(Long id) {
        return wxGoodsMapper.selectWxGoodsById(id);
    }

/*    @Override
    public WxGoods selectWxGoodsByName(String name) {
        return wxGoodsMapper.selectWxGoodsByName(name);
    }*/


    /***
     * 通过商品id查询【商品】
     * 和上面有所区分，这是给后端人员返回的数据，多带了‘成本’、‘概率’两项数据
     * @param id
     * @return
     */
    @Override
    public WxGoods selectWxGoodsById4Vue(Long id) {
        return wxGoodsMapper.selectWxGoodsById4Vue(id);
    }

    /***
     * 查询在这个集合内的元素
     * @param list
     * @return
     */
    @Override
    public List<WxGoodsVo> selectWxGoodsByRowNums(List<Integer> list) {
        List<WxGoods> wxGoodsList = wxGoodsMapper.selectWxGoodsByRowNums(list);
        List<WxGoodsVo> wxGoodsVos = new ArrayList<>(10);
        for (WxGoods wxGoods : wxGoodsList) {
            WxGoodsVo wxGoodsVo = new WxGoodsVo();
            BeanUtils.copyBeanProp(wxGoodsVo,wxGoods);
            wxGoodsVos.add(wxGoodsVo);
        }
        return wxGoodsVos;
    }


    /***
     * 查看商品有无库存
     * @param id 商品id
     * @return 查询结果，是否为0
     */
    @Override
    public int selectGoodsCount(Long id) {
        return wxGoodsMapper.selectGoodsCount(id);
    }

    /***
     * 新增商品
     * @param wxGoods 商品对应的实体类
     * @return 影响行数对应的AjaxResult
     */
    @Override
    public int addGoods(WxGoods wxGoods) {
        LoginUser loginUser = SpringUtils.getBean(TokenService.class).getLoginUser(ServletUtils.getRequest());
        String username = loginUser.getUsername();
        wxGoods.setCreateBy(username);
        wxGoods.setUpdateBy(username);
        return wxGoodsMapper.addGoods(wxGoods);
    }

    /***
     * 获取所有的商品,需要联查wx_image获取商品对应的image
     * 还需要联查wx_goods_attr获取商品对应的参数
     * @return
     */
    @Override
    public List<WxGoods> findAllGoods(WxGoods wxGoods) {
        List<WxGoods> wxGoodsList = wxGoodsMapper.findAllGoods(wxGoods);
        if (!CollectionUtils.isEmpty(wxGoodsList)){
            for (WxGoods goods : wxGoodsList) {
                ArrayList<WxImageVo> wxImageVos = new ArrayList<>();
                ArrayList<WxGoodsAttrVo> wxGoodsAttrVos = new ArrayList<>();
                for (WxImage wxImage : goods.getWxImageList()) {
                    WxImageVo wxImageVo = new WxImageVo();
                    BeanUtils.copyBeanProp(wxImageVo,wxImage);
                    wxImageVos.add(wxImageVo);
                }
                for (WxGoodsAttr wxGoodsAttr : goods.getWxGoodsAttrList()) {
                    WxGoodsAttrVo wxGoodsAttrVo = new WxGoodsAttrVo();
                    BeanUtils.copyBeanProp(wxGoodsAttrVo,wxGoodsAttr);
                    wxGoodsAttrVos.add(wxGoodsAttrVo);
                }
                if (!CollectionUtils.isEmpty(goods.getWxImageList())){
                    goods.setWxImageVoList(wxImageVos);
                }
                if (!CollectionUtils.isEmpty(goods.getWxGoodsAttrList())){
                    goods.setWxGoodsAttrVoList(wxGoodsAttrVos);
                }
            }

        }
        return wxGoodsList;
    }


    /***
     * 查找不在该小程序码对应的商品池的那些商品，给后台页面使用
     * @param id
     * @return
     */
    @Override
    public List<WxGoodsVo> findAllGoodsNotInQr(Long id) {
        return wxGoodsMapper.findAllGoodsNotInQr(id);
    }


    /***
     * 查找该小程序码对应的商品池的那些商品，给后台页面使用
     * @param id
     * @return
     */
    @Override
    public List<WxGoodsVo> findAllGoodsInQr(Long id) {
        return wxGoodsMapper.findAllGoodsInQr(id);
    }

    /***
     * 更新商品
     * @param wxGoods 商品类
     * @return 影响行数
     */
    @Override
    public int updateWxGoods(WxGoods wxGoods) {
        return wxGoodsMapper.updateWxGoods(wxGoods);
    }

    /***
     * 删除商品,逻辑上级联删除，即 商品所关联的图片和属性都删除
     * @param id 商品id
     * @return
     */
    @Override
    public int deleteWxGoodsById(Long id) {
        return wxGoodsMapper.deleteWxGoodsById(id);
    }

    /**
     * 查询商品列表
     * @param wxGoods
     * @return
     */
    @Override
    public List<WxGoods> selectWxGoodsList(WxGoods wxGoods) {
        return wxGoodsMapper.findAllGoods(wxGoods);
    }
}
